package com.flk.flkelasticsearch.es;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.searchbox.core.SearchResult;
import lombok.Builder;
import lombok.Data;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 解析Suggest信息
 *
 * @author xxwy
 * on 2018/11/14 0014
 */
public class SearchResultSuggest {

    private static final String OPTIONS_KEY = "options";
    private static final String SUGGEST_KEY = "suggest";
    private static final String _INDEX_KEY = "_index";
    private static final String _TYPE_KEY = "_type";
    private static final String _ID_KEY = "_id";
    private static final String _SCORE_KEY = "_score";
    private static final String _SOURCE_KEY = "_source";
    private static final String INPUT_KEY = "input";


    public static List<String> parseSuggestResponse(SearchResult searchResult,String suggestName,String field) {
        if (searchResult != null) {
            JsonObject suggest = searchResult.getJsonObject().getAsJsonObject(SUGGEST_KEY);
            if (suggest != null && suggest.has(suggestName)) {
                JsonObject suggest_info = suggest.getAsJsonArray(suggestName).get(0).getAsJsonObject();
                if(suggest_info != null ) {
                    JsonArray options = suggest_info.getAsJsonArray(OPTIONS_KEY);
                    if (options != null && options.size() != 0) {
                        return extractSuggest(options,field);
                    }
                }
            }
        }
        return null;
    }



    private static ArrayList<String> extractSuggest(JsonArray options, String field) {
        ArrayList<String> words = new ArrayList<>();
        if (options.isJsonArray()) {
            Iterator var1 = options.getAsJsonArray().iterator();
            while (var1.hasNext()){
                JsonObject option = (JsonObject)var1.next();
                String index = option.get(_INDEX_KEY).getAsString();
                String type = option.get(_TYPE_KEY).getAsString();
                String id = option.get(_ID_KEY).getAsString();
                Double score = null;
                if (option.has(_SCORE_KEY) && !option.get(_SCORE_KEY).isJsonNull()) {
                    score = option.get(_SCORE_KEY).getAsDouble();
                }
                JsonObject sources = option.getAsJsonObject(_SOURCE_KEY).getAsJsonObject(field);
                if(sources != null){
                    if(sources.isJsonArray()){
                        //循环查询input
                        Iterator<JsonElement> var2 = sources.getAsJsonArray().iterator();
                        while (var1.hasNext()) {
                            JsonObject source = (JsonObject)var2.next();
                            words.add(extractInputWords(source));
                        }
                    }else {
                        words.add(extractInputWords(sources));
                    }
                }
            }
            return words;
        }
        return words;
    }

    private static String extractInputWords(JsonObject source) {
        return source.get(INPUT_KEY).getAsString();
    }

    @Data
    @Builder
    public static class Options {
        public String text;
        public String index;
        public String type;
        public String id;
        public Double score;
        public List<String> words;
    }
}
